﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.UnitOfWork;
using MvcMall.Framework.Utility;

namespace MvcMall.AdminWeb.Manage.AdminUsers
{
    public partial class ChangePassword : ManageBasePage
    {
        private IAdminUserTask _adminUserTask;
        protected void Page_Load(object sender, EventArgs e)
        {
            _adminUserTask = GetInstance<IAdminUserTask>();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var password = txtPassword.Text;
            var oldPassword = txtOldPassword.Text;

            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(oldPassword))
            {
                ShowMsg("输入不完整");
                return;
            }

            var user = _adminUserTask.GetUser(LogOnUser);
            var oldHashPassword = CryptTools.HashPassword(oldPassword);
            if (user.Password != oldHashPassword)
            {
                ShowMsg("旧的密码不正确");
                return;
            }

            using (var work = UnitOfWork.Begin())
            {
                if (_adminUserTask.ChangePassword(user.Id, password) != null)
                {
                    work.Commit();
                    ShowMsg("修改密码成功");
                }
                else
                {
                    ShowMsg("修改密码失败");
                }
            }

        }
    }
}